package com.zc.lodop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zc.base.Result;
import com.zc.lodop.service.SendService;
import com.zc.lodop.sqlbean.Sender;
import com.zc.lodop.sqlbean.Sto;

@Controller
@RequestMapping("/lodop")
public class LodopController{
	
	@Autowired
	private SendService senderService;
    
    @RequestMapping("/sto.do")
    public ModelAndView getSenderList(Sto sto, String senderNameForSerach ,HttpServletRequest req) {
    	ModelAndView m = new ModelAndView();
    	m.addObject("sto",sto);
    	try{
    		Sender sender = new Sender();
    		if(!"".equals(senderNameForSerach)){
    			sender.setSenderName(senderNameForSerach);
    		}
    		List<Sender> list = senderService.selectList(sender);
    		m.addObject("list",list);
    		m.setViewName("../views/index");
    	}catch (Exception e){
    		m.setViewName("error");
    	}
    	return m;
    }
    
    @RequestMapping("/insert.do")
    @ResponseBody
    public Result insertSender(Sender sender, HttpServletRequest req) {
    	Result ret = new Result();
    	try{
    		Sender countSender = new Sender();
    		countSender.setSenderName(sender.getSenderName());
    		int count = senderService.selectCount(countSender);
    		if(count==0){
    			senderService.insertSelective(sender);
    		}else{
    			ret.setCodeResult("100", "User already exists");
    		}
    	}catch (Exception e){
    		ret.setCodeResult("100", "System error");
    	}
    	return ret;
    }
}
